/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.wicketstuff.ki.authz;

import org.apache.wicket.authorization.IAuthorizationStrategy;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authorization.IUnauthorizedComponentInstantiationListener;
import org.apache.wicket.Component;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.ki.SecurityUtils;
import org.wicketstuff.ki.example.SignInPage;

/**
 * Ki-specific implementation of the IAuthorizationStrategy interface.
 *
 * @author Les Hazlewood
 * @since 13 Oct 2008
 */
public class KiAuthorizationStrategy implements IAuthorizationStrategy, IUnauthorizedComponentInstantiationListener {

    public <T extends Component> boolean isInstantiationAuthorized(Class<T> componentClass) {

        //you can check in any number of ways here, perhaps checking for roles or permissions:

        boolean authorized = SecurityUtils.getSubject().hasRole("someRole");

        //or check based on permissions:

        authorized = SecurityUtils.getSubject().isPermitted("someComponent:instantiate");

        return authorized;
    }

    public boolean isActionAuthorized(Component component, Action action) {
        //TODO - perform authorization check using roles or permission checks, just like above
        return true;
    }

    public void onUnauthorizedInstantiation(Component component) {
        //TODO - page is application specific - make configurable:
        throw new RestartResponseAtInterceptPageException(SignInPage.class);
    }
}
